<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>用户登录</title>
    <style>
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            margin: 0;
            padding: 0;
            color: #333;
            min-height: 100vh;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            background-attachment: fixed;
        }

        .container {
            max-width: 500px;
            margin: 3rem auto;
            padding: 2rem;
        }

        h1 {
            color: white;
            margin-bottom: 2rem;
            font-weight: 600;
            text-align: center;
            text-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
        }

        .login-container {
            background: rgba(255, 255, 255, 0.95);
            border-radius: 12px;
            padding: 2.5rem;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.15);
            backdrop-filter: blur(5px);
        }

        .form-group {
            margin-bottom: 1.5rem;
            text-align: left;
        }

        label {
            display: block;
            margin-bottom: 0.5rem;
            font-weight: 500;
            color: #334155;
        }

        input[type="text"],
        input[type="password"] {
            width: 100%;
            padding: 0.8rem 1rem;
            border: 1px solid #e2e8f0;
            border-radius: 8px;
            font-size: 1rem;
            transition: all 0.3s;
            box-sizing: border-box;
        }

        input[type="text"]:focus,
        input[type="password"]:focus {
            outline: none;
            border-color: #3b82f6;
            box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.2);
        }

        button[type="submit"] {
            width: 100%;
            background-color: #3b82f6;
            color: white;
            border: none;
            padding: 0.8rem;
            border-radius: 8px;
            font-weight: 500;
            cursor: pointer;
            transition: all 0.3s;
            font-size: 1rem;
            margin-top: 1rem;
        }

        button[type="submit"]:hover {
            background-color: #2563eb;
            transform: translateY(-2px);
        }

        .nav-links {
            display: flex;
            justify-content: center;
            gap: 1.5rem;
            margin-top: 1.5rem;
        }

        .nav-links a {
            color: white;
            text-decoration: none;
            font-weight: 500;
            transition: all 0.2s;
            text-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
        }

        .nav-links a:hover {
            color: #e2e8f0;
            text-decoration: underline;
        }

        .error-message {
            color: #ef4444;
            margin-bottom: 1rem;
            text-align: center;
            font-size: 0.9rem;
        }
    </style>
</head>
<body>
<div class="container">
    <h1>用户登录</h1>
    <div class="login-container">
        <!-- 错误消息显示 -->
        <div class="error-message" id="errorMessage" th:if="${param.error}">
            用户名或密码错误，请重试
        </div>

        <form id="loginForm">
            <div class="form-group">
                <label for="username">用户名</label>
                <input id="username" name="username" placeholder="请输入用户名" required type="text"/>
            </div>
            <div class="form-group">
                <label for="password">密码</label>
                <input id="password" name="password" placeholder="请输入密码" required type="password"/>
            </div>
            <button type="submit">登录</button>
        </form>
    </div>
    <div class="nav-links">
        <a th:href="@{/register}">注册新用户</a>
    </div>
</div>
<script>
    // 在登录表单的提交事件处理前添加这个函数
    function handleLoginSuccess(data) {
        // 存储token
        localStorage.setItem('authToken', data.token);

        // 处理重定向
        const urlParams = new URLSearchParams(window.location.search);
        window.location.href = urlParams.get('redirect') || '/interview';
    }

    document.getElementById('loginForm').addEventListener('submit', async function (e) {
        e.preventDefault();

        const username = document.getElementById('username').value;
        const password = document.getElementById('password').value;

        try {
            const response = await fetch('/api/auth/login', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify({username, password})
            });

            if (!response.ok) {
                throw new Error('登录失败');
            }

            const data = await response.json();

            // 存储认证信息
            localStorage.setItem('authToken', data.token);
            localStorage.setItem('user', JSON.stringify(data.user));

            // 跳转到主页面
            window.location.href = '/interview';

        } catch (error) {
            alert('登录失败: ' + error.message);
        }
    });

    // 检查URL中的logout参数
    const urlParams = new URLSearchParams(window.location.search);
    if (urlParams.has('logout')) {
        const errorMessage = document.getElementById('errorMessage');
        errorMessage.textContent = '您已成功登出';
        errorMessage.style.color = '#10b981'; // 绿色表示成功
    }
</script>
</body>
</html>